//package com.gitee.mazhenggg.demo.gateway.config;
//
//import io.grpc.ManagedChannel;
//import io.grpc.ManagedChannelBuilder;
//import io.opentelemetry.api.OpenTelemetry;
//import io.opentelemetry.api.common.Attributes;
//import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
//import io.opentelemetry.context.propagation.ContextPropagators;
//import io.opentelemetry.exporter.jaeger.JaegerGrpcSpanExporter;
//import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter;
//import io.opentelemetry.sdk.OpenTelemetrySdk;
//import io.opentelemetry.sdk.resources.Resource;
//import io.opentelemetry.sdk.trace.SdkTracerProvider;
//import io.opentelemetry.sdk.trace.SpanProcessor;
//import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
//import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.env.Environment;
//
//import java.util.concurrent.TimeUnit;
//
//@Configuration
//public class TraceConfig {
//    private static final String ENDPOINT_V2_SPANS = "/api/v2/spans";
//
//    @Value("${spring.application.name}")
//    private String applicationName;
//    //@Bean
//    public OpenTelemetry openTelemetry() {
//        //SpanProcessor spanProcessor = getOtlpProcessor();
//        SpanProcessor spanProcessor = getZipkinProcessor();
//        Resource serviceNameResource = Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, applicationName));
//
//        // Set to process the spans by the Zipkin Exporter
//        SdkTracerProvider tracerProvider =
//                SdkTracerProvider.builder()
//                        .addSpanProcessor(spanProcessor)
//                        .setResource(Resource.getDefault().merge(serviceNameResource))
//                        .build();
//        OpenTelemetrySdk openTelemetry =
//                OpenTelemetrySdk.builder().setTracerProvider(tracerProvider)
//                        .setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
//                        .buildAndRegisterGlobal();
//
//        // add a shutdown hook to shut down the SDK
//        Runtime.getRuntime().addShutdownHook(new Thread(tracerProvider::close));
//
//        // return the configured instance so it can be used for instrumentation.
//        return openTelemetry;
//    }
//
//    private SpanProcessor getZipkinProcessor() {
//        String host = "localhost";
//        int port = 9411;
//        String httpUrl = String.format("http://%s:%s", host, port);
//        ZipkinSpanExporter zipkinExporter = ZipkinSpanExporter.builder().setEndpoint(httpUrl + ENDPOINT_V2_SPANS).build();
//        return SimpleSpanProcessor.create(zipkinExporter);
//    }
//    //@Bean
//    public OpenTelemetry initOpenTelemetry() {
//        String jaegerHost = "10.5.1.186";
//        int jaegerPort = 14250;
//        // Create a channel towards Jaeger end point
//        ManagedChannel jaegerChannel =
//                ManagedChannelBuilder.forAddress(jaegerHost, jaegerPort).usePlaintext().build();
//        // Export traces to Jaeger
//        JaegerGrpcSpanExporter jaegerExporter =
//                JaegerGrpcSpanExporter.builder()
//                        .setChannel(jaegerChannel)
//                        .setTimeout(30, TimeUnit.SECONDS)
//                        .build();
//
//        Resource serviceNameResource =
//                Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, applicationName));
//
//        // Set to process the spans by the Jaeger Exporter
//        SdkTracerProvider tracerProvider =
//                SdkTracerProvider.builder()
//                        .addSpanProcessor(SimpleSpanProcessor.create(jaegerExporter))
//                        .setResource(Resource.getDefault().merge(serviceNameResource))
//                        .build();
//        OpenTelemetrySdk openTelemetry =
//                OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).build();
//
//        // it's always a good idea to shut down the SDK cleanly at JVM exit.
//        Runtime.getRuntime().addShutdownHook(new Thread(tracerProvider::close));
//
//        return openTelemetry;
//    }
//
//
//}